import registnet.records.grade.GradeSheet;
import registnet.records.grade.source.GradeSheetSource;
import registnet.records.Student;
import registnet.records.Grade;
		
import registnet.dao.GradePeer;
import registnet.dao.CoursePeer;
import registnet.dao.UnitPeer;
		
import resources.Constants;

/* [start] 	initializes gui variables */
String _gradesheet = bundle.getProperty("registnet.module.grades.gradesheet");

String _academic_year = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.academic_year");
String _semester = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.semester");
String _course = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.course");
String _section = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.section");

String _student_number = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.student_number");
String _full_name = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.full_name");
String _sex = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.sex");
String _college = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.college");
String _degree_course = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.degree_course");
String _grade = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.grade");

List courses;
if (_user_type.equals(Constants.COLLEGE))
 	courses = new CoursePeer().getCourseByCollege(new CollegePeer().getCollegeId(_office));
 else if(_user_type.equals(Constants.UNIT))
 	courses = new CoursePeer().getCoursesByUnit(new UnitPeer().getUnitId(_office));
 
/* [end] */		



/* [start]	defines a renderer for the grades */
private class GradeSheetRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		new Label(data[0]).setParent(row);
		new Label(data[1]).setParent(row);
		new Label(data[2]).setParent(row);
		new Label(data[3]).setParent(row);
		new Label(data[4]).setParent(row);
		new Label(data[5]).setParent(row);
		new Label(data[6]).setParent(row);
	}
}
/* [end] */



/* [start] 	retrieves the grades and places them on the listbox */
void retrieveGrades(){
	String courseValue = course_code.value;
	String sectionValue = section.value;
	Integer semesterValue = Constants.MAP_SEMESTER_KEY.get(semester.getSelectedItem().getLabel());
	Integer yearValue;
	if (semesterValue == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
		yearValue = Integer.parseInt(year2.value);
	else
		yearValue = Integer.parseInt(year1.value);
	GradeSheet gradeSheet = new GradeSheetSource(courseValue, sectionValue, semesterValue, yearValue).getGradeSheet();
	List grades = gradeSheet.getGrades();
	ListIterator iter = grades.listIterator();
	String[][] listModel = new String[grades.size()][7];
	if (_user_type.equals(Constants.REGISTRAR) || new CoursePeer().getCourse(courseValue).getUnit().getCollege().getCollegeCode().equals(_office) || new CoursePeer().getCourse(courseValue).getUnit().getUnitCode().equals(_office)){
		int i = 0;
		while(iter.hasNext()){
			Grade grade = iter.next();
			Student student = grade.getStudent();
			listModel[i][0] = "" + (i + 1);
			listModel[i][1] = student.getStudentNumber();
			listModel[i][2] = student.getLastName() + ", " + student.getFirstName() + " " + student.getMiddleName();
			listModel[i][3] = student.getSex();
			listModel[i][4] = student.getCurriculum().getDegreeProgram().getCollege().getCollegeCode();
			listModel[i][5] = student.getCurriculum().getDegreeProgram().getDegreeCode();
			listModel[i][6] = Constants.MAP_GRADE_VALUE.get(grade.getGrade());
			i++;
		}
		ListModel strset = new SimpleListModel(listModel);
		contents_win.getFellow("gradesheet_g").setModel(strset);
		contents_win.getFellow("gradesheet_g").setRowRenderer(new GradeSheetRenderer());
	}
	else
		alert("Gradesheet cannot be viewed by this account");
}
/* [end] */



/* [start]	defines a renderer for the grades */
public class SectionRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data).setParent(item);
	}
}
/* [end] */



/* [start] 	retrieves the sections and places them on the listbox */
void retrieveSections(){
	String courseValue = course_code.value;
	//String sectionValue = section.value;
	Integer semesterValue = Constants.MAP_SEMESTER_KEY.get(semester.getSelectedItem().getLabel());
	Integer yearValue;
	if (semesterValue == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
		yearValue = Integer.parseInt(year2.value);
	else
		yearValue = Integer.parseInt(year1.value);
	List sections = new GradePeer().getGradedSectionNames(new CoursePeer().getCourseId(courseValue), yearValue, semesterValue);
	
	ListModel strset = new ListModelList(sections);
	section_lb.setModel(strset);
	section_lb.setItemRenderer(new SectionRenderer());
}
/* [end] */

public class UIItemRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data).setParent(item);
	}
}

void retrieveCourses(){
	String[] listModel = new String[courses.size()];
	for (int i=0; i<courses.size(); i++)
		listModel[i] = courses.get(i).getCourseCode();
	ListModel strset = new ListModelList(listModel);
	course_code_lb.setModel(strset);
	course_code_lb.setItemRenderer(new UIItemRenderer());
}

void filterCourse(String string){
	ListIterator i = courses.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next().getCourseCode(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	course_code_lb.getModel().clear();
	course_code_lb.getModel().addAll(list);
}



/* [start]	automatically updates the 'to' value of academic year based on the
			current 'from' value of the academic year */
private void updateAY(){
	try{
		year2.value = (Integer.parseInt(event.value) + 1).toString();
	}catch(Exception e) {year1.value = null; year2.value = null;}
}
/* [end] */
